'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'shiftform', 
        'datetimepicker', 'datetimepickerCN'], function ($, validate) {
  return {

    init: function () {
      
      this.existBrandCNCode = false;
      this.existSimcard = false;
      
      this.initPage();
      this.initCreate();
      this.exist();
      
    },

    initPage: function () {
      
      $('select[name="carOwnership"]').select2({
        theme: "classic",
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true,
      });

      $('select[name="transMean"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true,
      });
      
      $('select[name="vehicleType"]').select2({
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true,
      });
      
      $('.date').datetimepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        language: 'zh-CN',
        todayBtn: 'linked',
        startDate: '${today}',
        todayHighlight: true,
        pickerPosition: 'bottom-right',
        minView: 'month'
      });
      
      $('.month').datetimepicker({  
        format: 'yyyy-mm',
        autoclose: true,  
        weekStart: 1,  
        startView: 3,  
        minView: 3,  
        forceParse: false,  
        language: 'zh-CN'  
      });
      
    },
    
    initCreate: function () {
      
      var that = this;

      var $form = $(document).find('form');
      $form.bt_validate();            //bind validate
      
      $form.shiftform({               //bind shiftform
        beforeSubmit: function (el) {
          
          $form.trigger("validate.ajax.submit");
          
          if (that.existBrandCNCode || that.existSimcard) {
            return false;
          }
          
          return $.bt_validate.result[$.bt_validate.form_id];
          
        }
      })
      
    },
    
    exist: function() {
      
      var that = this;
      var $btnSubmit = $('button.btn-submit');
      
      // 校验牵引车中文车牌号是否存在
      $(document).on('blur', 'input[name="brandCNCode"]', function(e) {
        
        e.preventDefault();
        
        var $brandCNCode = $('input[name="brandCNCode"]');
        var brandCNCode = $brandCNCode.val().trim();
        if (!brandCNCode) return;
          
        $.ajax({
          'url': window.__CTX__.root + "/tractors/exist",
          'data': {
            'category': 'brandCNCode',
            'brandCNCode': brandCNCode
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.exist) {
            $brandCNCode.show_err_tooltip('该牵引车中文牌号已存在！');
            that.existBrandCNCode = true;
            return false;
          }
          
          that.existBrandCNCode = false;
          
        }).fail(function(){
          $brandCNCode.show_err_tooltip('查询牵引车中文牌号失败！');
          $btnSubmit.prop('disabled', true);
        });

      });
      
      // 校验车辆SimCard是否已存在
      $(document).on('blur', 'input[name="simCard"]', function(e) {
        
        e.preventDefault();
        
        var $simCard = $('input[name="simCard"]');
        var simCard = $simCard.val().trim();
        if (!simCard) return;
          
        $.ajax({
          'url': window.__CTX__.root + "/tractors/exist",
          'data': {
            'category': 'simCard',
            'simCard': simCard
          }
        }).done(function(res){
          
          res = typeof res === 'String' ? JSON.parse(res) : res;
          if (res.data.exist) {
            $simCard.show_err_tooltip(res.data.message);
            that.existSimcard = true;
            return false;
          }

          that.existSimcard = false;
          
        }).fail(function(){
          $simCard.show_err_tooltip('查询Sim卡号失败！');
          $btnSubmit.prop('disabled', true);
        });

      });
      
    }
    
  }
})